/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmf.vista;

import com.cmf.controladores.ControladorJuego;
import com.cmf.controladores.Funciones;
import com.cmf.model.Jugador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.Timer;


/**
 *
 * @author Grupos
 */
public class Dados extends javax.swing.JFrame {

    /**
     * Creates new form Dados
     */
    private Timer timer, timer1;
    private int delay;
    private int num;
    private EscaleraClaret jFrame;
    private Jugador jugador;

    public Dados() {
        Fondo fondo=new Fondo("afro6.jpg");
        initComponents();
        initTimer();// Inicializa los Temporizadores
        fondop.setBorder(fondo.getBorder());
        timer.start(); //Empieza la animacion del dado
        if (delay==2000){
            timer.stop();
            dado.setIcon(new ImageIcon(getClass().getResource("/com/cmf/recursos/imagenes/dados1.jpg")));
        }

    }
    public Dados(EscaleraClaret jFrame, Jugador jugador) {
        this.jugador=jugador;
        this.jFrame=jFrame;
        jFrame.setEnabled(false);
        initComponents();
        initTimer();//inicia el temporizador para la animacion de lanzamiento de los dados
        Fondo fondo=new Fondo("afro6.jpg");
        fondop.setBorder(fondo.getBorder());
        ImageIcon imagen=new ImageIcon(getClass().getResource("/com/cmf/recursos/imagenes/claret.JPG"));
        setIconImage(imagen.getImage());
        timer.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fondop = new javax.swing.JPanel();
        continuar = new javax.swing.JButton();
        dado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        fondop.setBackground(new java.awt.Color(204, 0, 0));

        continuar.setText("Cuntinuar");
        continuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                continuarActionPerformed(evt);
            }
        });
        continuar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                continuarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout fondopLayout = new javax.swing.GroupLayout(fondop);
        fondop.setLayout(fondopLayout);
        fondopLayout.setHorizontalGroup(
            fondopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, fondopLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(dado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(43, 43, 43))
            .addGroup(fondopLayout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addComponent(continuar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(96, 96, 96))
        );
        fondopLayout.setVerticalGroup(
            fondopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondopLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(dado, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(continuar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(41, 41, 41))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-268)/2, (screenSize.height-291)/2, 268, 291);
    }// </editor-fold>//GEN-END:initComponents

    private void continuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continuarActionPerformed
        // setea el avance del juego, pasa de nuevo al vista del juego y cuenta los cuadros
        dispose();
        if (jFrame!=null){
            jFrame.setAvance(num);
            jFrame.setVisible(true);
            jFrame.setEnabled(true);
            jugador.setAvance(num);
            
            new ControladorJuego(jFrame.getNumeros(), jFrame.getPaneles(), num, jugador,jFrame).contarCuadros();//llama el controlador del juego
        }
            
    }//GEN-LAST:event_continuarActionPerformed

    private void continuarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_continuarKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            dispose();
        if (jFrame!=null){
            jFrame.setAvance(num);
            jFrame.setVisible(true);
            jFrame.setEnabled(true);
            jugador.setAvance(num);

            new ControladorJuego(jFrame.getNumeros(), jFrame.getPaneles(), num, jugador,jFrame).contarCuadros();//llama el controlador del juego
        }
        }
    }//GEN-LAST:event_continuarKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Dados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Dados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Dados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Dados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Dados().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton continuar;
    private javax.swing.JLabel dado;
    private javax.swing.JPanel fondop;
    // End of variables declaration//GEN-END:variables

    private void initTimer(){
        Funciones f=new Funciones();
        num=f.azar();
        timer = new Timer (100, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                timer1.start();
                delay=delay+200;
                if (delay==3000){
                    timer1.stop();
                    timer.stop();
                    dado.setIcon(new ImageIcon(getClass().getResource("/com/cmf/recursos/imagenes/dados"
                            + num+".jpg")));
                }
            }
        });
        
        timer1=new Timer(50, new ActionListener() {
            int num=1;
            public void actionPerformed(ActionEvent e) {
               dado.setIcon(new ImageIcon(getClass().getResource("/com/cmf/recursos/imagenes/dados"+num+".jpg")));
               num=num+1;
               if(num==6)
                   num=1;
            }
        });
    }

}
